<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('商品列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>标题：</label>
                                <input type="text" name="title"/>
                            </li>
                            <li>
                                <label>商品编号：</label>
                                <input type="text" name="productNo"/>
                            </li>
                            <li>
                                <label>状态：</label>
                                <select name="status">
                                    <option value="">所有</option>
                                    <option value="1">上架</option>
                                    <option value="2">下架</option>
                                </select>
                            </li>
                            <li>
                                <label>供货商：</label>
                                <select name="supplierId" class="form-control">
                                    <option value="">-请选择-</option>
                                    <option th:each="supplier : ${shopSupplierList}" th:value="${supplier.id}" th:text="${supplier.name}"></option>
                                </select>
                            </li>
                            <li>
                                <label>是否特价：</label>
                                <select name="isSpecial">
                                    <option value="">所有</option>
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </li>
                            <li>
                                <label>是否新品：</label>
                                <select name="isNew">
                                    <option value="">所有</option>
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="shop:product:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="shop:product:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="shop:product:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="shop:product:export">
                    <i class="fa fa-download"></i> 导出
                </a>

                <a class="btn btn-info" onclick="$.table.importExcel('importTpl',400, 320)" shiro:hasPermission="shop:product:import">
                    <i class="fa fa-upload"></i> 商品导入
                </a>

                <a class="btn btn-info" onclick="importSyncPriceExcel('importTpl2',400, 320)" shiro:hasPermission="shop:product:import">
                    <i class="fa fa-upload"></i> 价格同步导入
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('shop:product:edit')}]];
        var removeFlag = [[${@permission.hasPermi('shop:product:remove')}]];
        var uploadImageFlag = [[${@permission.hasPermi('shop:product:upload:image')}]];
        var prefix = ctx + "shop/product";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                importUrl: prefix + "/importData",
                importTemplateUrl: prefix + "/importTemplate/1",
                importTemplateUrl2: prefix + "/importTemplate/2",
                importPriceUrl: prefix + "/importPriceData",
                modalName: "商品",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '商品ID',
                    visible: false
                },
                {
                    field: 'categoryName',
                    title: '分类'
                },
                {
                    field: 'title',
                    title: '标题'
                },
                {
                    field: 'productNo',
                    title: '商品编号'
                },
                {
                    field: 'thumbnail',
                    title: '缩略图',
                    formatter: function(value, row, index) {
                        if(value != null){
                            return $.table.imageView(value, "100%", "750");
                        } else {

                        }
                    }
                },
                {
                    field: 'specs',
                    title: '产品规格'
                },
                {
                    field: 'orderNumber',
                    title: '排序编号'
                },
                {
                    field: 'price',
                    title: '商品价格'
                },
                {
                    field: 'originPrice',
                    title: '原始价格'
                },
                {
                    field: 'limitedPrice',
                    title: '限时优惠价'
                },
                {
                    field: 'limitedTime',
                    title: '限时优惠截止时间'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                        if(1==value){
                            return "<span class=\"badge badge-primary\">上架</span>";
                        } else {
                            return "<span class=\"badge badge-danger\">下架</span>"
                        }
                    }
                },
                {
                    field: 'viewCount',
                    title: '访问量'
                },
                {
                    field: 'realViewCount',
                    title: '真实访问量'
                },
                {
                    field: 'salesCount',
                    title: '销售量'
                },
                {
                    field: 'supplierName',
                    title: '供货商'
                },
                {
                    field: 'isNew',
                    title: '是否新品',
                    formatter: function(value, row, index) {
                        if(1==value){
                            return "<span class=\"badge badge-primary\">是</span>";
                        } else {
                            return "否"
                        }
                    }
                },
                {
                    field: 'isSpecial',
                    title: '是否特价',
                    formatter: function(value, row, index) {
                        if(1==value){
                            return "<span class=\"badge badge-primary\">是</span>";
                        } else {
                            return "否"
                        }
                    }
                },
                {
                    field: 'realSalesCount',
                    title: '真实的销售量'
                },
                {
                    field: 'stock',
                    title: '剩余库存'
                },
                {
                    field: 'remark',
                    title: '备注信息'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs ' + uploadImageFlag + '" href="javascript:void(0)" onclick="uploadImage(\'' + row.title + '\',\'' + row.id + '\')"><i class="fa fa-image"></i>上传图</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function uploadImage(title ,id) {
            if(id == undefined){
                return;
            }
            if(title == undefined || title == ""){
                title = "上传图";
            } else {
                title += " -上传图";
            }

            var url = prefix + "/upload/image?productId="+id;
            if ($.common.isMobile()) {
                $.modal.openFull(title, url);
            } else {
                $.modal.open(title, url);
            }
        }

        //价格同步
        function importSyncPriceExcel(formId, width, height) {
            table.set();
            var currentId = $.common.isEmpty(formId) ? 'importTpl2' : formId;
            var _width = $.common.isEmpty(width) ? "400" : width;
            var _height = $.common.isEmpty(height) ? "320" : height;
            top.layer.open({
                type: 1,
                area: [_width + 'px', _height + 'px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: '导入价格同步数据',
                content: $('#' + currentId).html(),
                btn: ['<i class="fa fa-check"></i> 导入', '<i class="fa fa-remove"></i> 取消'],
                // 弹层外区域关闭
                shadeClose: true,
                btn1: function(index, layero){
                    var file = layero.find('#file2').val();
                    if (file == '' || (!$.common.endWith(file, '.xls') && !$.common.endWith(file, '.xlsx'))){
                        $.modal.msgWarning("请选择后缀为 “xls”或“xlsx”的文件。");
                        return false;
                    }
                    var index = top.layer.load(2, {shade: false});
                    $.modal.disable();
                    var formData = new FormData(layero.find('form')[0]);
                    $.ajax({
                        url: table.options.importPriceUrl,
                        data: formData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        type: 'POST',
                        success: function (result) {
                            if (result.code == web_status.SUCCESS) {
                                $.modal.close(index);
                                $.modal.closeAll();
                                $.modal.alertSuccess(result.msg);
                                $.table.refresh();
                            } else if (result.code == web_status.WARNING) {
                                $.modal.close(index);
                                $.modal.enable();
                                $.modal.alertWarning(result.msg)
                            } else {
                                $.modal.close(index);
                                $.modal.enable();
                                $.modal.alertError(result.msg);
                            }
                        }
                    });
                }
            });
        }
    </script>

</body>

<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <font color="red" class="pull-left mb10">
                注意：点击了"导入"数据就会导入到系统，请勿重复操作（重复操作导致数据重复添加）。
            </font>
            <label class="control-label is-required">供货商：</label>
            <select name="supplierId" class="form-control" style="width: auto;" required>
                <option value="">-请选择-</option>
                <option th:each="supplier : ${shopSupplierList}" th:value="${supplier.id}" th:text="${supplier.name}"></option>
            </select>
            <input type="file" id="file" name="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/>
            <div class="mt10 pt5">
                <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>

<!-- 导入区域 -->
<script id="importTpl2" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <font color="red" class="pull-left mb10">
                注意：点击了"导入"数据就会导入到系统，请勿重复操作（重复操作导致数据重复添加）。
            </font>
            <label class="control-label is-required">供货商：</label>
            <select name="supplierId" class="form-control" style="width: auto;" required>
                <option value="">-请选择-</option>
                <option th:each="supplier : ${shopSupplierList}" th:value="${supplier.id}" th:text="${supplier.name}"></option>
            </select>
            <input type="file" id="file2" name="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/>
            <div class="mt10 pt5">
                <a onclick="$.table.downloadImportTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>

</html>